/**
 * 
 */
package com.xtourms.client.sys.um;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.frame.MainFrame;
import com.xtourms.client.ui.util.MessageBox;

/**
 * @author xujh
 *
 */
public class LoginPanel extends Composite {

	private static LoginPanelUiBinder uiBinder = GWT
			.create(LoginPanelUiBinder.class);

	interface LoginPanelUiBinder extends UiBinder<Widget, LoginPanel> {
	}

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final UserManagerServiceAsync loginService = GWT
			.create(UserManagerService.class);
	
	@UiField TextBox login_name;
	@UiField PasswordTextBox login_pwd;
	@UiField Button login_button;
	
	/**
	 * Because this class has a default constructor, it can
	 * be used as a binder template. In other words, it can be used in other
	 * *.ui.xml files as follows:
	 * <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 *   xmlns:g="urn:import:**user's package**">
	 *  <g:**UserClassName**>Hello!</g:**UserClassName>
	 * </ui:UiBinder>
	 * Note that depending on the widget that is used, it may be necessary to
	 * implement HasHTML instead of HasText.
	 */
	public LoginPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		login_name.setFocus(true);
	}
	
	@UiHandler("login_button")
	void handleLogin(ClickEvent evt){
		login_button.setEnabled(false);
		String userName = login_name.getValue();
		String pwd = login_pwd.getValue();
		loginService.login(userName, pwd, new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String userInfo) {
				if(!userInfo.isEmpty()){
					JSONObject loginInfo =(JSONObject) JSONParser.parse(userInfo);
					Context.ins.worker = (JSONObject)loginInfo.get("WORKER");
					Context.ins.location = (JSONArray)loginInfo.get("LOCATION");
					RootPanel.get().clear();
					MainFrame.getIns().reInit();
					RootPanel.get().add(MainFrame.getIns());
				} else {
					MessageBox.ins.showWarning("登录名字或密码错误，请重试!");
					reInit();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("登录服务异常 "+caught.getMessage());
				reInit();
			}		
		});
	}
	
	public void reInit() {
		login_name.setValue("");
		login_pwd.setValue("");
		login_button.setEnabled(true);
		login_name.setFocus(true);
	}
}
